package com.lottery.game.controller.v2.notice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lottery.game.common.Response;
import com.lottery.game.common.utils.BasePageVO;
import com.lottery.game.constants.RespErrorCodes;
import com.lottery.game.controller.v2.notice.request.KfRoomVO;
import com.lottery.game.controller.v2.notice.request.KfTitleVO;
import javafx.scene.media.Media;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.lottery.game.annotation.Current;
import com.lottery.game.core.model.dto.UserToken;
import com.lottery.game.core.modules.entity.user.ChatRecord;
import com.lottery.game.core.modules.entity.user.CustomerService;
import com.lottery.game.core.modules.entity.user.GameCustomerNotice;
import com.lottery.game.core.modules.mapper.KfglDao;
import com.lottery.game.core.modules.service.app.ChatRecordService;
import com.lottery.game.core.modules.service.game.SysDictService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/kfgl")
@Api(tags="APP-在线聊天客服接口")
@Slf4j
public class KfglController {
	@Autowired
	private ChatRecordService chatRecordService;
	
    @Autowired
    private SysDictService sysDictService;
	
	@Autowired
	KfglDao kdao;
	@RequestMapping(method=RequestMethod.POST,value="/getKfgl")
	@ApiOperation(value="查询客服公告",notes="查询客服公告")
	public Response<PageInfo<GameCustomerNotice>> getKfgl(@RequestBody BasePageVO param) {
		try {
			PageHelper.startPage(param.getPage(),param.getRows());
			PageInfo<GameCustomerNotice> listPage = new PageInfo<>(kdao.getAllKfGg());
			return Response.build(listPage);
		}catch (Exception e){
			log.error("查询客服公告发送错误", e);
			return Response.buildError(RespErrorCodes.UNKNOWN_ERROR.getErrorMsg());
		}
	}
	
	@RequestMapping(method=RequestMethod.POST,value="/getKf",produces = { MediaType.APPLICATION_JSON_UTF8_VALUE } )
	@ApiOperation(value="查询客服信息",notes="查询客服信息(type为0：微信客服，1：QQ客服)")
	public Response<PageInfo<CustomerService>> getKf(@RequestBody BasePageVO param) {
		try {
			PageHelper.startPage(param.getPage(),param.getRows());
			PageInfo<CustomerService> listPage = new PageInfo<>(kdao.getAllKf());
			List<CustomerService> list = listPage.getList();
			if(!CollectionUtils.isEmpty(list)){
				for (CustomerService customerService : list) {
					customerService.setUrl(sysDictService.findImageServerAddress()+customerService.getUrl());
				}
			}
			return Response.build(listPage);
		}catch (Exception e){
			log.error("查询客服信息发送错误", e);
			return Response.buildError(RespErrorCodes.UNKNOWN_ERROR.getErrorMsg());
		}

	}
	
	
	@RequestMapping(method=RequestMethod.POST,value="/getKfll")
	@ApiOperation(value="查询聊天记录",notes="查询聊天记录")
	 @ApiImplicitParams({
	    	@ApiImplicitParam(name = "ticket", value = "令牌",dataType = "String",paramType="header",required = true)
	    })
	public Response<List<ChatRecord>> getKfll(@ApiIgnore @Current UserToken ticket,
											  @RequestBody KfRoomVO param) {
		try {
			PageHelper.startPage(param.getPage(),param.getRows());
			PageInfo<ChatRecord> listPage = new PageInfo<>(chatRecordService.queryChatRecordByRoomnumberByUser(param.getRoomNumber(),param.getPage(),param.getRows(), ticket.getId()));
			return Response.build(listPage);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询客服信息发送错误", e);
			return Response.buildError(RespErrorCodes.UNKNOWN_ERROR.getErrorMsg());
		}
	}


	@RequestMapping(method=RequestMethod.POST,value="/getKftitleByLike")
	@ApiOperation(value="查询客服公告标题",notes="查询客服公告标题")
	public Response<PageInfo<GameCustomerNotice>> getKftitleByLike(@RequestBody KfTitleVO param) {
		try {
			PageHelper.startPage(param.getPage(),param.getRows());
			PageInfo<GameCustomerNotice> listPage = new PageInfo<>(kdao.getKftitleByLike(param.getTitle()));
			return Response.build(listPage);
		}catch (Exception e){
			log.error("查询客服公告标题发送错误", e);
			return Response.buildError(RespErrorCodes.UNKNOWN_ERROR.getErrorMsg());
		}
	}
}
